import json


class Logic:
    def __init__(self, mongo_client, stranger_limit, card_limit):
        self.m_client = mongo_client
        self.stranger_limit = stranger_limit
        self.card_limit = card_limit

        self.config_D = self.m_client.config_D
        self.config_J = self.m_client.config_J
        self.config_A = self.m_client.config_A

        self.box_ip_D = self.m_client.box_ip_D
        self.box_ip_J = self.m_client.box_ip_J
        self.box_ip_A = self.m_client.box_ip_A

        self.func_client_D = self.m_client.func_client_D
        self.func_client_J = self.m_client.func_client_J
        self.func_client_A = self.m_client.func_client_A

    #  返回档案P
    def acvs_p(self, data_info, similarity, picture_image):
        # 三次ovn
        repos_j = [self.config_J.get("ID1"), self.config_J.get("ID2"), self.config_J.get("ID3")]
        repos_d = [self.config_D.get("ID1"), self.config_D.get("ID2"), self.config_D.get("ID3")]
        repos_a = [self.config_A.get("ID1"), self.config_A.get("ID2"), self.config_A.get("ID3")]

        # 证件照库ovn
        card_ovn_data = self.card_ovn(similarity, repos_j, picture_image)

        # 路人库ovn
        stranger_ovn_data = self.stranger_ovn(similarity, repos_d, picture_image)

        # 档案库ovn
        acvs_ovn_data = self.acvs_ovn(similarity, repos_a, picture_image)
        # 合并路人 档案
        for a_face_id in acvs_ovn_data:
            if a_face_id not in stranger_ovn_data:
                stranger_ovn_data.append(a_face_id)

        # 获取证件照档案p
        card_p_lists = self.get_card_acvs_p(card_ovn_data)
        # 获取路人库档案p
        stranger_p_lists = self.get_stranger_acvs_p(stranger_ovn_data, data_info)

        return card_p_lists + stranger_p_lists


    def get_card_acvs_p(self, card_ovn_data):
        card_p_list = []
        for card_id in card_ovn_data:
            card_info = {"card_id": card_id, "p": None}
            self.m_client.get_cardacvs(card_info, card_p_list)
        return card_p_list

    def get_stranger_acvs_p(self, stranger_ovn_data, data_info):
        stranger_p_list = []
        for stranger_face_id in stranger_ovn_data:
            face_info = {"face_id": stranger_face_id, "p": None}
            face_info.update(data_info)
            self.m_client.get_faceacvs(face_info, stranger_p_list)

        return stranger_p_list

    # 证件ovn
    def card_ovn(self, similarity, repo_id: list, face_b64):

        if self.box_ip_J == "0.0.0.0":
            return []

        ovn_data = self.func_client_J.one_than_n(
            image=face_b64,
            threshold=similarity,
            repositories=repo_id,
            topk=self.card_limit
        ).get("message")

        # 证件ovn列表
        card_face_info = []
        for res in ovn_data.get("retrieval_results"):
            # 比中证件
            for face in res.get("similar_faces"):
                hit_face_id = json.loads(face.get("extra_meta"))["face_id"]
                # similarity = face.get("similarity")
                card_face_info.append(hit_face_id)
        return card_face_info

    # 路人ovn
    def stranger_ovn(self, similarity, repo_id, face_b64):
        ovn_data = self.func_client_D.one_than_n(
            image=face_b64,
            threshold=similarity,
            repositories=repo_id,
            topk=self.stranger_limit
        ).get("message")

        stranger_list = []
        for res in ovn_data.get("retrieval_results"):
            # 路人
            for face in res.get("similar_faces"):
                hit_face_id = json.loads(face.get("extra_meta"))["face_id"]
                stranger_list.append(hit_face_id)

        return stranger_list

    # 档案库ovn
    def acvs_ovn(self, similarity, repo_id, face_b64):
        ovn_data = self.func_client_A.one_than_n(
            image=face_b64,
            threshold=similarity,
            repositories=repo_id,
            topk=self.stranger_limit
        ).get("message")

        # 档案ovn列表
        acvs_list = []
        for res in ovn_data.get("retrieval_results"):
            # 路人
            for face in res.get("similar_faces"):
                hit_face_id = json.loads(face.get("extra_meta"))["face_id"]
                acvs_list.append(hit_face_id)

        return acvs_list
